﻿using Autofac;
using Autofac.Configuration;
using HHXX.DALFactory;
using HHXX.IBLL;
using HHXX.IDAL;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace HaoHaoCloud.Utility
{
    public class CustomAutofacModule : Autofac.Module
    {

        protected override void Load(ContainerBuilder containerBuilder)
        {
            //var str = typeof(IBaseDal<UserInfoSet>).AssemblyQualifiedName;
            #region 指定控制器也由autofac来进行实例获取
            var assembly = this.GetType().GetTypeInfo().Assembly;
            var builder = new ContainerBuilder();
            var manager = new ApplicationPartManager();
            manager.ApplicationParts.Add(new AssemblyPart(assembly));
            manager.FeatureProviders.Add(new ControllerFeatureProvider());
            var feature = new ControllerFeature();
            manager.PopulateFeature(feature);
            builder.RegisterType<ApplicationPartManager>().AsSelf().SingleInstance();
            //控制器支持属性注入
            builder.RegisterTypes(feature.Controllers.Select(ti => ti.AsType()).ToArray()).PropertiesAutowired();

            #endregion

            #region 注册实例
            //注册数据库会话实例
            containerBuilder.Register(r => DbSessionFactory.GetCurrentDbSession()).As<IDbSession>().PropertiesAutowired();
            //这种方法会确保系统中的单例实例最终转化为由容器托管的单例实例
            //containerBuilder.RegisterInstance(DbSessionFactory.GetCurrentDbSession()).ExternallyOwned();

            #endregion

            #region Autofac基于配置文件的服务注册

            //Json格式配置文件 Dals
            IConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
            configurationBuilder.AddJsonFile("Config/dals.json");
            IConfigurationRoot root = configurationBuilder.Build();
            //开始读取配置文件里的内容信息
            ConfigurationModule module = new ConfigurationModule(root);

            //Second Json file Services
            IConfigurationBuilder configurationBuilderService = new ConfigurationBuilder();
            configurationBuilderService.AddJsonFile("Config/services.json");
            IConfigurationRoot rootService = configurationBuilderService.Build();
            ConfigurationModule moduleService = new ConfigurationModule(rootService);

            //根据配置文件的内容注册服务
            containerBuilder.RegisterModule(module);
            containerBuilder.RegisterModule(moduleService);

            //IContainer container = containerBuilder.Build();
            //ITestServiceA testServiceA = container.Resolve<ITestServiceA>();
            //var db = container.Resolve<DbSession>();
            //一个可选的参数P被提供给构造函数
            //var dbA = container.Resolve<IUserInfoDal>(new NamedParameter("",""));
            //var dbC = container.ResolveNamed<IUserInfoDal>("UserInfoDal");
            //var dbD = container.ResolveKeyed<IUserInfoDal>("UserInfoDal");
            //var dbE = container.Resolve<IUserInfoDal>();

            //var dbB = container.Resolve<IUserInfoService>();

            #endregion



        }

    }
}
